package com.vg.view;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vg.com.chatonline.activity.R;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import br.com.dina.ui.widget.UITableView;
import ce51.dunghn.imagethread.JsonParse;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class info_User extends FragmentActivity {
	private static Fragment instace = null;
	private UITableView tableView;
	ImageView img;
	String[] imageUrls;
	ArrayList<HashMap<String, String>> imagelist = new ArrayList<HashMap<String, String>>();
	String url = "http://android.dzanchoi.vn/api/images/list.xmob?id=";
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;

	// public static Fragment newinstance() {
	// if (instace == null)
	// instace = new info_User();
	// return instace;
	// }

	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info_user);
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);
		// TODO Auto-generated method stub
		img = (ImageView) findViewById(R.id.img_info_user);

		options = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.no)
				.showStubImage(R.drawable.no).cacheInMemory().cacheOnDisc()
				.bitmapConfig(Bitmap.Config.RGB_565).build();

		DisplayMetrics metrics = this.getResources().getDisplayMetrics();
		int height = metrics.heightPixels;
		int width = metrics.widthPixels;
		LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(width,
				height / 3);
		img.setLayoutParams(param);
		// img_1_3 post_img = (img_1_3) vi.findViewById(R.id.layout_info_user_);
		// post_img.fillData("http://res.vtc.vn/media/vtcnews/2012/08/10/chipu6.jpg");
		imagelist = loadImage(16);
		HashMap<String, String> hm = new HashMap<String, String>();
		imageUrls = new String[imagelist.size()];
		for (int i = 0; i < imagelist.size(); i++) {
			hm = imagelist.get(i);
			imageUrls[i] = hm.get("images");
		}
		
		@SuppressWarnings("deprecation")
		Gallery gallery = (Gallery) findViewById(R.id.info_gallery);
		gallery.setAdapter(new ImagePagerAdapter(imageUrls));
		gallery.setSelection(1);
		
		// Set su kien khi click vao anh, mo ra activity moi de xem anh
		gallery.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(), ViewSingleImage.class);
				i.putExtra("IMAGE", imageUrls[arg2]);
				startActivity(i);
			}
		});

		tableView = (UITableView) findViewById(R.id.listview_info_user);
		createList();
		tableView.commit1(R.layout.list_item_top, R.layout.list_item_bottom,
				R.layout.list_item_middle);

	}

	private void createList() {
		tableView.addBasicItem("Tên:");
		tableView.addBasicItem("Ngày Sinh");
		tableView.addBasicItem("Giới Tính:");
		tableView.addBasicItem("Nơi Sống");
	}

	// Tra ve list anh tu json data
	public ArrayList<HashMap<String, String>> loadImage(int page) {
		ArrayList<HashMap<String, String>> imagelist = new ArrayList<HashMap<String, String>>();
		JSONArray data = new JSONArray();
		JsonParse js = new JsonParse();
		JSONObject jso = new JSONObject();
		jso = js.getJSONFromUrl(url + page);
		try {
			data = jso.getJSONArray("data");
			for (int i = 0; i < data.length(); i++) {
				JSONObject obj = data.getJSONObject(i);
				String image = obj.getString("images");
				HashMap<String, String> arr = new HashMap<String, String>();
				arr.put("images", image);

				imagelist.add(arr);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return imagelist;
	}

	// Adapter cua gallery
	private class ImagePagerAdapter extends BaseAdapter {

		private String[] images;
		private LayoutInflater inflater;

		ImagePagerAdapter(String[] images) {
			this.images = images;
			inflater = getLayoutInflater();
		}

		public int getCount() {
			return images.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = (ImageView) convertView;
			if (imageView == null) {
				imageView = (ImageView) inflater.inflate(
						R.layout.info_item_gallery, parent, false);
			}
			imageLoader.displayImage(images[position], imageView, options);
			return imageView;
		}
	}
}
